import { defineStore } from 'pinia';
import { getAccountInfoSync, getEnv } from '@tarojs/taro';

// 安全地获取小程序信息，避免在H5环境中报错
function getMiniProgramInfo() {
  try {
    const env = getEnv();
    if (env === 'WEAPP') {
      return getAccountInfoSync();
    }
    return { miniProgram: { envVersion: 'release' } }; // 默认返回正式环境
  } catch (error) {
    console.warn('getAccountInfoSync not available in current environment:', error);
    return { miniProgram: { envVersion: 'release' } }; // 默认返回正式环境
  }
}

// 获取小程序信息，用于环境判断
getMiniProgramInfo();

type company = { id: string, departName: string, mobile: string, fax: string, logo?: string, memo?: string }; // 对象

interface NurseState {
  // showSelectHospital: Boolean | undefined,
  hospitals: [],
  // hospital: { name: string, id: string },
  company: company,

  imageUrl: string,

  qrCodeUrl: string
  // addrlist: [],
  // guardianlist: []
}

interface NurseState {
  // showSelectHospital: Boolean | undefined,
  hospitals: [],
  // hospital: { name: string, id: string },
  company: company,

  imageUrl: string
  // addrlist: [],
  // guardianlist: []
}

export const useNurseStore = defineStore('nurse-store', {
  state: (): NurseState => ({
    hospitals: [],
    company: {
      id: "",
      departName: '',
      mobile: '',
      fax: "",
      memo: ""
    },
    qrCodeUrl: '',
    imageUrl: 'https://anfine.huasun-healthcare.com/api', //正式环境
    //imageUrl: 'http://127.0.0.1:8080/jeecg-boot', //开发环境
  }),
  getters: {
    // getShowSelectHospital: state => state.showSelectHospital,
    getHospitals: state => state.hospitals,
  },
  actions: {
    setHospitals(hospitals: []) {
      this.hospitals = hospitals
    },
    setCompany({ id, departName, mobile, fax, memo }: company) {
      this.company.id = id
      this.company.departName = departName
      this.company.mobile = mobile
      this.company.fax = fax
      this.company.memo = memo
    },
    setLogo(logo: string) {
      this.company.logo = logo
    },
  }
});
